package com.stadium.controller;

import com.stadium.common.BaseResponse;
import com.stadium.dto.BasicsRuleDto;
import com.stadium.service.BasicsRuleSV;
import com.stadium.vo.BasicsRuleVo;
import com.stadium.vo.searchVo.BasicsRuleSearchVo;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 规则表
 *
 * @author zq
 * @email 513973172@qq.com
 * @date 2020-01-31 15:13:40
 */
@RestController
@RequestMapping("/api/basicsrule")
@Api(tags = "规则表")
public class BasicsRuleCL{
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private BasicsRuleSV basicsRuleSV;

    @ApiOperation(value = "保存或修改")
    @PostMapping(value = "/v1/save")
    public BaseResponse save(@Validated BasicsRuleVo basicsRuleVo) {
        String id = "";
        String message = "";
        try {
            if (basicsRuleVo.getId() == null ||basicsRuleVo.getId() == ""){
                    basicsRuleSV.save(basicsRuleVo);
                message = "添加成功";
            }else{
                    basicsRuleSV.update(basicsRuleVo);
                message = "修改成功";
            }
            return BaseResponse.ok(message);

        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/v1/delete")
    @ApiImplicitParams({@ApiImplicitParam(name="id",value="id")})
    public BaseResponse delete(@RequestParam(name = "id", required = true)String id) {
        try {
                basicsRuleSV.delete(id);
            return BaseResponse.ok();
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "批量删除")
    @PostMapping(value = "/v1/deleteBatch")
    @ApiImplicitParams({@ApiImplicitParam(name="ids",value="ids")})
    public BaseResponse deleteBatch(@RequestParam(name = "ids", required = true)String[] ids) {
        try {
                basicsRuleSV.deleteBatch(ids);
            return BaseResponse.ok();
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "获得列表")
    @GetMapping(value = "/v1/query")
    public BaseResponse<List<BasicsRuleDto>> query(@Validated BasicsRuleSearchVo searchVo) {
        try {
            if (StringUtils.isNotEmpty(searchVo.getSearchValue())) {
                searchVo.setSearchValue("%" + searchVo.getSearchValue() + "%");
            }
            List<BasicsRuleDto> queryList = basicsRuleSV.query(searchVo);
            int total = basicsRuleSV.getCount(searchVo);
            return BaseResponse.ok(queryList, total);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "获得详情", notes = "获得详情")
    @GetMapping(value = "/v1/get")
    @ApiImplicitParams({@ApiImplicitParam(name="id",value="id")})
    public BaseResponse<BasicsRuleDto> get(@RequestParam(name = "id", required = true)String id) {
        try {
                BasicsRuleDto queryData = basicsRuleSV.get(id);
            return BaseResponse.ok(queryData);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "获取联赛下的比赛规则", notes = "获取联赛下的比赛规则")
    @GetMapping(value = "/v1/getByEventId")
    @ApiImplicitParams({@ApiImplicitParam(name="eventId",value="eventId")})
    public BaseResponse<BasicsRuleDto> getByLeagueId(@RequestParam(name = "eventId", required = true)String eventId) {
        try {
            BasicsRuleDto queryData = basicsRuleSV.getByLeagueId(eventId);
            return BaseResponse.ok(queryData);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }
}
